package com.cypher.encryptionanddecryption.fragment.protocol.nb25;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.cypher.encryptionanddecryption.R;
import com.cypher.encryptionanddecryption.activity.BluetoothCommunicationActivity;
import com.cypher.encryptionanddecryption.bean.CommunicationBean;
import com.cypher.encryptionanddecryption.fragment.protocol.AbstractProtocolFragment;
import com.cypher.encryptionanddecryption.util.HexUtils;
import com.cypher.encryptionanddecryption.util.Nb25ProtocolUtils;

import org.apache.commons.lang3.StringUtils;

/**
 * 密钥参数页面
 * @author: 古霸天
 * @date: 2020/12/14
 */
public class SecretKeyParamFragment extends AbstractProtocolFragment {
    private final String EMPTY = "";
    /**
     * 服务器地址入框
     */
    private EditText keyVersionTxt;
    /**
     * 端口号输入框
     */
    private EditText keyValueTxt;
    private CommunicationBean bean;
    private BluetoothCommunicationActivity activity;
    public SecretKeyParamFragment(BluetoothCommunicationActivity activity, CommunicationBean bean) {
        this.activity = activity;
        this.bean = bean;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_nb25_secret_key_param, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
    }

    /**
     * 初始化
     * @param v
     */
    private void init(View v) {

        keyVersionTxt = v.findViewById(R.id.secret_key_version);
        keyVersionTxt.setInputType(InputType.TYPE_CLASS_NUMBER);

        // 定义输入框过滤器
        InputFilter[] inputFilters = new InputFilter[1];
        // 设置输入框输入内容长度
        inputFilters[0] = new InputFilter.LengthFilter(32);
        keyValueTxt = v.findViewById(R.id.secret_key_value);
        keyValueTxt.setFilters(inputFilters);
    }

    @Override
    public void fillFragment(String data) {
        // 清空内容
        keyVersionTxt.setText("");
        keyValueTxt.setText("");

        Runnable r = () -> {
            reader(Nb25ProtocolUtils.formatReceiveData(data));
        };
        activity.getHandler().postDelayed(r, 200);
    }
    @Override
    public String getHexCode(String rwType) {
        if (StringUtils.equals("read", rwType)) {
            return Nb25ProtocolUtils.getReadCommand(bean.getHexCode(), PROTOCOL_VERSION);
        }
        // 组包
        return getData();
    }

    /**
     * 渲染页面
     * @param data
     */
    private void reader(String data) {
        keyVersionTxt.setText(String.valueOf(Integer.parseInt(data.substring(2, 4), 16)));
        keyValueTxt.setText(data.substring(4, 36));
    }

    /**
     * 获取数据域
     * @return
     */
    private String getData() {
        String keyVersion = keyVersionTxt.getText().toString();
        if (StringUtils.isEmpty(keyVersion)) {
            Toast.makeText(activity, "密钥版本不能为空", Toast.LENGTH_SHORT).show();
            return EMPTY;
        }
        String keyValue = keyValueTxt.getText().toString();
        if (StringUtils.isEmpty(keyValue)) {
            Toast.makeText(activity, "密钥值不能为空", Toast.LENGTH_SHORT).show();
            return EMPTY;
        }
        if (keyValue.length() < 32) {
            Toast.makeText(activity, "密钥值长度不够32位", Toast.LENGTH_SHORT).show();
            return EMPTY;
        }

        StringBuffer data = new StringBuffer();
        data.append(HexUtils.paddingLeft(Integer.toHexString(keyValue.length() / 2), 2));
        data.append(HexUtils.paddingLeft(Integer.toHexString(Integer.parseInt(keyVersion)), 2));
        data.append(HexUtils.paddingRight(keyValue, 64));
        return Nb25ProtocolUtils.getWriteCommand(bean.getHexCode(), data.toString(), PROTOCOL_VERSION);
    }
}
